﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Net;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using BaseHelper;
using BaseHelper.Base;
using BaseHelper.Text;

namespace DeviceSDK.LED.NuoWa
{
    class NuoWa_PSD100
    {
        private static bool IsSync = true;
        //private static bool IsConnected;
        private static bool Sending = false;
        private static bool IsInit = false;
        private static object IsBusying = false;
        private static NuoWa_Msg msg;
        //private static int ret = -999;        1.28 批量发布，暂时屏蔽公共变量
        //private static NP_CARD_INFO cardInfo;
        private static int s_MsgID = 0x800;
        private static SortedList<string, CardStatus> slIpCardInfo = new SortedList<string, CardStatus>();


        private class CardStatus
        {
            public bool IsConnected;
            public string MacID;

            public CardStatus(string macID,bool isConnected)
            {
                MacID = macID;
                IsConnected = isConnected;
            
            }
        }

        //获取错误代码
        public static string Get_Error(int ret)
        {
            switch (ret)
            {
                case 0:
                    return "操作成功！";
                case 1:
                    return "操作失败！";
                case 2:
                    return "命令执行失败！";
                case 3:
                    return "保存节目单到磁盘失败！";

                case 4:
                    return "有素材文件格式不支持！";
                case -1:
                    return "本设备不支持此命令！";
                
                default:
                    return "未知错误！";
            }
        }

        private delegate void DefWndProcHanlder();
        //private event DefWndProcHanlder DefWndProcEvent;

        public static void OnLEDNotify(System.Windows.Forms.Message m)
        {
            int ret =  -1;
            int ptr = (int)m.WParam;
            Console.WriteLine("返回消息：" + ptr);

            switch (ptr)
            {
                case (int)CustomMessageType.WM_CardDisconnect:
                    string cardID = Marshal.PtrToStringBSTR(m.LParam);
                    break;
                //case (int)CustomMessageType.WM_CardInfo:
                //    //NP_CARD_INFO cardInfo;
                //    //cardInfo = (NP_CARD_INFO)Marshal.PtrToStructure(m.LParam, typeof(NP_CARD_INFO));
                //    //CompleteConnectCardList(cardInfo);
                //    //GetCardInfo(cardInfo);
                //    break;
                case (int)CustomMessageType.WM_SendInsertPlay:
                    ret = (int)m.LParam;
                    //Console.WriteLine("PSD100 发送插播：" + ret.ToString());
                    break;
                case (int)CustomMessageType.WM_SendNotify:
                    ret = (int)m.LParam;
                   // Console.WriteLine("PSD100 发送提示：" + ret.ToString());
                    break;
                case (int)CustomMessageType.WM_SendPlayProgram:
                    ret=(int)m.LParam;
                    //Console.WriteLine("PSD100 发送节目单：" + ret.ToString());
                    break;
            }


        }

      
        //初始化
        public static int Init(string IP)
        {
            Sending = false;

            if (LEDHelper.Release_IsOrderToSend)
            {
                Random rnd = new Random();
                while ((bool)IsBusying)
                {
                    Thread.Sleep(rnd.Next(1000));
                    Application.DoEvents();
                }
                lock (IsBusying)
                {
                    IsBusying = true;
                }
            }


            if (!slIpCardInfo.ContainsKey(IP) || !slIpCardInfo[IP].IsConnected)
            {
                try
                {
                    if (!IsInit)
                    {
                        msg = new NuoWa_Msg();
                        string curIP = LEDHelper.Release_Local_IP;// GetAvaliableIPAddress();//
                        if (NuoWa_SDK_PSD100.NP_Initialize(msg.GetHandle, s_MsgID, curIP, 29000, LEDHelper.TempFullPath + "NovaPluto"))
                        {
                            IsInit = true;
                        }
                        else
                        {
                            Close(IP);
                            return 1;
                        }
                    }

                    if (IsInit)
                    {
                        NP_CARD_INFO cardInfoTmp;

                        //诺瓦的PSD100，有的时候请求2次，才可以连接上
                        if (NuoWa_SDK_PSD100.NP_ConnectCardOfLocalNet(IP, IsSync, out cardInfoTmp))
                        {
                            if (!slIpCardInfo.ContainsKey(IP))
                                slIpCardInfo.Add(IP, new CardStatus(cardInfoTmp.ID, true));
                            else
                                slIpCardInfo[IP].IsConnected = true;

                            return 0;
                        }
                        else
                        {
                            AppHelper.Sleep(1500);
                            if (NuoWa_SDK_PSD100.NP_ConnectCardOfLocalNet(IP, IsSync, out cardInfoTmp))
                            {
                                if (!slIpCardInfo.ContainsKey(IP))
                                    slIpCardInfo.Add(IP, new CardStatus(cardInfoTmp.ID, true));
                                else
                                    slIpCardInfo[IP].IsConnected = true;

                                return 0;
                            }
                            else
                            {
                                Close(IP);
                                return 1;
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    LogHelper.Err("RT.Helper.Device.LED.NuoWa.NuoWa_PSD100.cs", "Init()", ex);
                   
                    Close(IP);
                    return 1;
                }
            }
            else
                return 0;

            return 1;
        }

        private static int WaitReturn(string IP)
        {
            return 0;
            int ret = -999;
            //1.28 修改成批量的
            int TimeOut = 50;
            while (ret == -999 && TimeOut-- > 0 || Sending)
            {
                Thread.Sleep(100);
                Application.DoEvents();
            }
            if (ret == -999 && TimeOut < 0)
                ret = 2;
            Close(IP);
            return ret;
        }


        private static void Close(string IP)
        {
            try
            {
                //NuoWa_SDK.NP_UnInitialize();
            }
            catch 
            {
            }
            finally
            {
                //slIpCardInfo[IP].IsConnected = false;
                
                IsBusying = false;
            }

        }

        private static string GetAvaliableIPAddress()
        {
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            string strIp;

            foreach (IPAddress ip in ips)
            {
                strIp = ip.ToString();
                #region 判断IP格式是否正确（排除win7和vista下的乱地址IP
                string[] ipPart = strIp.Split('.');
                if (ipPart.Length == 4)
                {
                    int partValue;
                    if (int.TryParse(ipPart[0], out partValue) && int.TryParse(ipPart[1], out partValue)  && int.TryParse(ipPart[2], out partValue)  && int.TryParse(ipPart[3], out partValue))    {

                        return strIp;
                    }
                }

                #endregion
            }
            return "";
        }


        //重启
        public static int Get_ScreenCap(string IP,ref Image Img)
        {
            int ret = Init(IP);
            if (ret != 0)
                return ret;

            string tempPath = LEDHelper.TempFullPath+ IP.Replace(".","_") +"_capscreen.png";
            if (NuoWa_SDK_PSD100.NP_GetCardScreenShotPicture(slIpCardInfo[IP].MacID, tempPath) && File.Exists(tempPath))
            {
                Img = Image.FromFile(tempPath);
                ret = 0;
            }
            else
                ret =1;

            Close(IP);

            return ret;
        }


        //重启
        public static int Set_Reboot(string IP)
        {
            int ret = Init(IP);
            if (ret != 0)
                return ret;

            ret = NuoWa_SDK_PSD100.NP_RestartCard(slIpCardInfo[IP].MacID) ? 0 : 1;
            Close(IP);

            return ret;
        }

        //获得-屏幕状态
        public static int Get_Screen_Status(string IP, ref int Value)
        {
          
            int ret = Init(IP);
            if (ret != 0)
                return ret;


            byte powerState;
            if (NuoWa_SDK_PSD100.NP_GetOnBoardPowerState(slIpCardInfo[IP].MacID, out powerState))
            {
                if (powerState >= 0 && powerState < 2)
                    ret = 0;
                else
                    ret = 1;    //失败
            }
            else
                ret = 2; //获取失败

            Close(IP);

            return ret;
        }

        //设置-屏幕-关闭
        public static int Set_Screen_Close(string IP)
        {
            int ret = Init(IP);
            if (ret != 0)
                return ret;

            ret = NuoWa_SDK_PSD100.NP_SetCardSreenStatus(slIpCardInfo[IP].MacID, (ushort)1) ? 0 : 1;

            Close(IP);
            return ret;

        }

        //设置-屏幕-开启
        public static int Set_Screen_Open(string IP)
        {
            int ret = Init(IP);
            if (ret != 0)
                return ret;

            ret = NuoWa_SDK_PSD100.NP_SetCardSreenStatus(slIpCardInfo[IP].MacID, (ushort)0) ? 0 : 1;
            Close(IP);
            return ret;
        }

        //获得-屏幕亮度
        public static int Get_Brightness(string IP, ref int Value)
        {
           int ret = Init(IP);
            if (ret != 0)
                return ret;

            byte brightValue;
            if (NuoWa_SDK_PSD100.NP_GetCardBrightValue(slIpCardInfo[IP].MacID, out brightValue))
                ret = (int)brightValue;
            else
                ret = 2;
            Close(IP);

            return ret;
        }

        //设置-屏幕亮度
        public static int Set_Brightness(string IP, int Value)
        {
            int ret = Init(IP);
            if (ret != 0)
                return ret;

            ret = NuoWa_SDK_PSD100.NP_SetCardBrightValue(slIpCardInfo[IP].MacID, Convert.ToByte(Value)) ? 0 : 1;
            Close(IP);
            return ret;
        }

        //设置-时间
        public static int Set_Time(string IP)
        {
            int ret = Init(IP);
            if (ret != 0)
                return ret;

            DateTime tempTime = DateTime.Now;
            SYSTEMTIME time = new SYSTEMTIME();
            time.wYear = (ushort)tempTime.Year;
            time.wMonth = (ushort)tempTime.Month;
            time.wDay = (ushort)tempTime.Day;
            time.wHour = (ushort)tempTime.Hour;
            time.wMinute = (ushort)tempTime.Minute;
            time.wSecond = (ushort)tempTime.Second;

            ret = NuoWa_SDK_PSD100.NP_SetCardSystemTime(slIpCardInfo[IP].MacID, time) ? 0 : 1;
            Close(IP);
            return ret;
        }

        //发送-节目
        public static int Send_Programs(bool IsTempPlaybill, string IP, int Width,int Height,PlaybillProgramInfo[] Programs)
        {
            int ret=Init(IP);
            if (ret != 0)
                return ret;


            ret = -999;

            if (IsTempPlaybill)
            {
                #region 临时插播
                NP_RECTANGLE windowRect = new NP_RECTANGLE();
                 NP_SIZE windowSize = new NP_SIZE();
                 NP_TIMESPAN playDuration = new NP_TIMESPAN();

                foreach (PlaybillProgramInfo pinfo in Programs)
                {
                   
                    windowSize.Width = pinfo.Width;
                    windowSize.Height = pinfo.Height;

                    playDuration.Hour = 0;
                    playDuration.Minute = 0;
                    playDuration.Second = pinfo.PlayTime;
                    playDuration.MilliSeconds = 0;


                    //LX_DeleteTheFile(0,pinfo.ScreenAreaID ,pinfo.ID,IP);

                    //int FileType = ToolsHelper.GetFileType(pinfo.FilePath);

                    switch (pinfo.Type)
                    {
                        case "picture":
                       
                          
                            NP_IMAGEFILE_INFO imageInfo = new NP_IMAGEFILE_INFO();
                            //imageInfo.BackMusicFileName = null;
                            //imageInfo.InEffectInfo.IsHasEffect = pinfo.Affect > 0 ? true : false;
                            //imageInfo.InEffectInfo.EffectValue = (ushort)(pinfo.Affect == 1 ? 0 : 1);
                            //imageInfo.InEffectInfo.EffectSpeed = (uint)pinfo.AffectTime * 10;
                            //imageInfo.OutEffectInfo.IsHasEffect = pinfo.Affect > 0 ? true : false;
                            //imageInfo.OutEffectInfo.EffectValue = (ushort)(pinfo.Affect == 1 ? 0 : 1);
                            //imageInfo.OutEffectInfo.EffectSpeed = (uint)pinfo.AffectTime*10;

                            
                            imageInfo.BackMusicFileName = null;
                            imageInfo.InEffectInfo.IsHasEffect =false;
                            imageInfo.InEffectInfo.EffectValue = 0;
                            imageInfo.InEffectInfo.EffectSpeed = 3;
                            imageInfo.OutEffectInfo.IsHasEffect = false;
                            imageInfo.OutEffectInfo.EffectValue = 0;
                            imageInfo.OutEffectInfo.EffectSpeed = 3;

                            
                            imageInfo.PlayScale = 0;
                            imageInfo.StayTime = (uint)LEDHelper.Release_Play_TimeOut*60;//(uint)pinfo.PlayTime;


                            if (!NuoWa_SDK_PSD100.NP_SendImageInsertPlay(slIpCardInfo[IP].MacID, pinfo.FilePath, windowSize, playDuration, imageInfo))
                            {
                                return 1;
                            }
                            
                            break;

                        case "video":

                            NP_VIDEOFIEL_INFO videoInfo = new NP_VIDEOFIEL_INFO();
                            videoInfo.PlayScale = 0;
                            videoInfo.Volume = 50;
                            if (!NuoWa_SDK_PSD100.NP_SendVideoInsertPlay(slIpCardInfo[IP].MacID, pinfo.FilePath, windowSize, playDuration, videoInfo))
                            {
                                return 1;
                            }
         
                            break;

                        case "rtf":

                            break;

                        case "text":
                            Font font = new Font(pinfo.FontName, pinfo.FontSize, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134)));
                            LOGFONTW logFont = new LOGFONTW();
                            logFont.lfCharSet = font.GdiCharSet;
                            logFont.lfFaceName = font.Name;
                            logFont.lfHeight = font.Height;
                            logFont.lfWidth = pinfo.FontSize;
                            logFont.lfWeight = pinfo.FontSize;
                            logFont.lfUnderline = 0;
                            font.Dispose();

                       
                            windowRect.X = pinfo.X;
                            windowRect.Y = pinfo.Y;
                            windowRect.Width = pinfo.Width;
                            windowRect.Height = pinfo.Height;

                            uint playTimes = 10;

                             NP_STATICTEXT_INFO staticTxtInfo = new NP_STATICTEXT_INFO();

                             staticTxtInfo.Text = pinfo.Text;
                             staticTxtInfo.TextFont.TextFont = logFont;
                             staticTxtInfo.BackColor = 0xffffff;
                           
                            //singleTxtInfo.TextEffect.EffectType = 0;
                            //singleTxtInfo.InEffectInfo.IsHasEffect = pinfo.Affect > 1 ? true : false;
                            //singleTxtInfo.InEffectInfo.EffectValue = (ushort)(pinfo.Affect==1?0:1);
                            //singleTxtInfo.InEffectInfo.EffectSpeed = (uint)pinfo.AffectTime;
                            //singleTxtInfo.OutEffectInfo.IsHasEffect = pinfo.Affect > 1 ? true : false;
                            //singleTxtInfo.OutEffectInfo.EffectValue = (ushort)(pinfo.Affect == 1 ? 0 : 1);
                            //singleTxtInfo.OutEffectInfo.EffectSpeed =  (uint)pinfo.AffectTime;

                            staticTxtInfo.TextEffect.EffectType = 0;
                            staticTxtInfo.RowSpacing = 0;
                            staticTxtInfo.CharacterSpacing = 1;
                            staticTxtInfo.AlignmentType = 2;
                            staticTxtInfo.PlayDuration.Hour = 99999999;
                            staticTxtInfo.PlayDuration.Minute = 5;
                            staticTxtInfo.PlayDuration.Second = 1;
                            staticTxtInfo.PlayDuration.MilliSeconds = 0;


                            if (!NuoWa_SDK_PSD100.NP_SendStaticTextNotify(slIpCardInfo[IP].MacID, windowRect, playTimes, staticTxtInfo))
                            {
                                return 1;
                            }
                            
                            break;
                    }
                    return WaitReturn(IP); ;
                }
                #endregion
            }
            else
            {
                #region 正常节目单

                //IntPtr _playProgramPtr = IntPtr.Zero;

                string fileName = LEDHelper.TempFullPath + "NuoWaPlayBill_" + IP + ".plym";

                NP_SIZE screenSize = new NP_SIZE();
                screenSize.Width = Width;
                screenSize.Height =Height;

                IntPtr playProgramPtr = NuoWa_SDK_PSD100.NP_CreatePlayProgram(fileName, screenSize);

                //播放时间
                NP_TIMESEGMENT_INFO timeSegmentInfo = new NP_TIMESEGMENT_INFO();
                timeSegmentInfo.Name = "Segment1";
                timeSegmentInfo.StartDate = new NP_DATE();
                timeSegmentInfo.StartDate.Year = DateTime.Now.Year;
                timeSegmentInfo.StartDate.Month = DateTime.Now.Month;
                timeSegmentInfo.StartDate.Day = DateTime.Now.Day;

                timeSegmentInfo.StopDate.Year = DateTime.Now.Year + 1;
                timeSegmentInfo.StopDate.Month = 1;
                timeSegmentInfo.StopDate.Day = 1;
                int size = Marshal.SizeOf(typeof(NP_ANALOGCLOCK_INFO));
                timeSegmentInfo.IsSpecificDate = false;
                timeSegmentInfo.IsSpecificDayOfWeek = true;
                timeSegmentInfo.IsWholeDayPlay = true;
                timeSegmentInfo.WeekDayIsValid = new bool[7] { true, true, true, true, true, true, true };
                timeSegmentInfo.StartTimeOfDay.Hour = 0;
                timeSegmentInfo.StartTimeOfDay.Minute = 0;
                timeSegmentInfo.StartTimeOfDay.Second = 1;
                timeSegmentInfo.StartTimeOfDay.MilliSeconds = 0;

                timeSegmentInfo.StopTimeOfDay.Hour = 0;
                timeSegmentInfo.StopTimeOfDay.Minute = 0;
                timeSegmentInfo.StopTimeOfDay.Second = 2;
                timeSegmentInfo.StopTimeOfDay.MilliSeconds = 0;

                if (!NuoWa_SDK_PSD100.NP_AddTimeSegment(playProgramPtr, timeSegmentInfo))
                    return 1;

                //添加页面
                NP_PAGE_INFO pageInfo = new NP_PAGE_INFO();
                pageInfo.Name = "page1";
                pageInfo.PlayType = 2;  // 播放方式：0表示指定时长， 1表示指定次数，2表示循环播放
                pageInfo.PlayTimes = 0;
                if (!NuoWa_SDK_PSD100.NP_AddBasicPage(playProgramPtr, 0, pageInfo))
                    return 1;


                SortedList<string, uint> slWindows = new SortedList<string, uint>();
                
                for(int i=0;i<Programs.Length;i++)
                {
                    PlaybillProgramInfo pinfo = Programs[i];
                    uint WinIndex = 0;
                    string WindowsID = pinfo.X.ToString() + "," + pinfo.Y.ToString() + "," + pinfo.Width.ToString() + "," + pinfo.Height.ToString();

                    if (!slWindows.ContainsKey(WindowsID))
                    {
                        //添加窗口
                        NP_WINDOW_INFO windowInfo = new NP_WINDOW_INFO();
                        windowInfo.Name = "W";
                        windowInfo.WindowRect = new NP_RECTANGLE();
                        windowInfo.WindowRect.X = pinfo.X;
                        windowInfo.WindowRect.Y = pinfo.Y;
                        windowInfo.WindowRect.Width = pinfo.Width;
                        windowInfo.WindowRect.Height = pinfo.Height;


                        if (!NuoWa_SDK_PSD100.NP_AddWindowToPage(playProgramPtr, 0, 0, windowInfo))
                            return 1;
                        WinIndex = (uint)slWindows.Count;
                        slWindows.Add(WindowsID, WinIndex);
                    }
                    else
                    {
                        WinIndex = slWindows[WindowsID];
                    }
                    switch (pinfo.Type)
                    {
                        case "picture":
                            NP_IMAGEFILE_INFO imageInfo = new NP_IMAGEFILE_INFO();
                            //imageInfo.BackMusicFileName = null;
                            //imageInfo.InEffectInfo.IsHasEffect = pinfo.Affect > 0 ? true : false;  //默认false
                            //imageInfo.InEffectInfo.EffectValue = (ushort)(pinfo.Affect == 1 ? 0 : 1);
                            //imageInfo.InEffectInfo.EffectSpeed = (uint)pinfo.AffectTime*10; //默认1
                            //imageInfo.OutEffectInfo.IsHasEffect = pinfo.Affect > 0 ? true : false;
                            //imageInfo.OutEffectInfo.EffectValue = (ushort)(pinfo.Affect == 1 ? 0 : 1);
                            //imageInfo.OutEffectInfo.EffectSpeed = (uint)pinfo.AffectTime * 10;
                            //imageInfo.PlayScale = 0;
                            //imageInfo.StayTime = (uint)pinfo.PlayTime;

                            imageInfo.BackMusicFileName = null;
                            imageInfo.InEffectInfo.IsHasEffect = false;
                            imageInfo.InEffectInfo.EffectValue = 0;
                            imageInfo.InEffectInfo.EffectSpeed = 1;
                            imageInfo.OutEffectInfo.IsHasEffect = false;
                            imageInfo.OutEffectInfo.EffectValue = 0;
                            imageInfo.OutEffectInfo.EffectSpeed = 1;
                            imageInfo.PlayScale = 0;
                            imageInfo.StayTime = (uint)pinfo.PlayTime;

                            if (!NuoWa_SDK_PSD100.NP_AddImageFile(playProgramPtr, 0, 0, WinIndex, pinfo.FilePath, imageInfo))
                            {
                                Console.WriteLine("RT.Helper.Device.LED.NuoWa.NuoWa_PSD100.cs->NP_AddImageFile");
                            }


                            break;

                        case "video":

                            NP_VIDEOFIEL_INFO videoInfo = new NP_VIDEOFIEL_INFO();
                            videoInfo.PlayScale = 0;
                            videoInfo.Volume = 10;
                            if (!NuoWa_SDK_PSD100.NP_AddVideoFile(playProgramPtr, 0, 0, WinIndex, pinfo.FilePath, videoInfo))
                            {
                                Console.WriteLine("RT.Helper.Device.LED.NuoWa.NuoWa_PSD100.cs->NP_AddVideoFile");
                            }

                            break;

                        case "rtf":

                            NP_TXTFILE_INFO txtInfo = new NP_TXTFILE_INFO();
                            txtInfo.BackColor = 0x000000;
                            imageInfo.BackMusicFileName = null;
                            txtInfo.InEffectInfo.IsHasEffect = false;
                            txtInfo.InEffectInfo.EffectValue = 0;
                            txtInfo.InEffectInfo.EffectSpeed = 1;
                            txtInfo.OutEffectInfo.IsHasEffect = false;
                            txtInfo.OutEffectInfo.EffectValue = 0;
                            txtInfo.OutEffectInfo.EffectSpeed = 1;
                            imageInfo.PlayScale = 0;
                            imageInfo.StayTime = (uint)pinfo.PlayTime;
                            txtInfo.ColorInverseType = 0;
                            if (!NuoWa_SDK_PSD100.NP_AddTxtFile(playProgramPtr, 0, 0, WinIndex, pinfo.FilePath, txtInfo))
                            {
                                Console.WriteLine("RT.Helper.Device.LED.NuoWa.NuoWa_PSD100.cs->NP_AddTxtFile");
                            }

                            break;

                        case "text":



                            //2018.11.13 因为PSD100发送文字只能背景，我自己写了一个测试
                            pinfo.Type = "picture";
                            pinfo.FilePath =TextToImage.CreateImage(LEDHelper.TempFullPath, pinfo.Text, pinfo.Width, pinfo.Height, pinfo.FontName, pinfo.FontSize, pinfo.FontColor);

                            Programs[i] = pinfo;
                            i--;



                            //2018.11.14 因为PSD100不支持文字，自己做了个函数生成 文字转图片
                            //Font font = new Font(pinfo.FontName, pinfo.FontSize, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134)));
                            //LOGFONTW logFont = new LOGFONTW();
                            //logFont.lfCharSet = font.GdiCharSet;
                            //logFont.lfFaceName = font.Name;
                            //logFont.lfHeight = font.Height;
                            //logFont.lfWidth = pinfo.FontSize;
                            //logFont.lfWeight = pinfo.FontSize;
                            //logFont.lfUnderline = 0;


                            //NP_STATICTEXT_INFO staticTxtInfo = new NP_STATICTEXT_INFO();
                            //staticTxtInfo.Text = pinfo.Text;
                            //staticTxtInfo.TextFont.TextFont = logFont;
                            //staticTxtInfo.BackColor = 0xffffffff;
                            //staticTxtInfo.TextEffect.EffectType = 0;
                            //staticTxtInfo.RowSpacing = 0;
                            //staticTxtInfo.CharacterSpacing = 1;
                            //staticTxtInfo.AlignmentType = 2;
                            //staticTxtInfo.VerAlignmentType = 2;

                            //staticTxtInfo.PlayDuration.Hour = 0;
                            //staticTxtInfo.PlayDuration.Minute = 0;
                            //staticTxtInfo.PlayDuration.Second = pinfo.PlayTime;
                            //staticTxtInfo.PlayDuration.MilliSeconds = 0;
                            //staticTxtInfo.InEffectInfo.IsHasEffect = false;
                            //staticTxtInfo.InEffectInfo.EffectValue = 14;
                            //staticTxtInfo.InEffectInfo.EffectSpeed = 20;

                            //if (!NuoWa_SDK.NP_AddStaticText(playProgramPtr, 0, 0, WinIndex, staticTxtInfo))
                            //{
                            //     Console.WriteLine("RT.Helper.Device.LED.NuoWa.NuoWa_PSD100.cs->NP_AddStaticText");
                            //}


                            break;
                    }
                }

                Thread.Sleep(100);
                Application.DoEvents();
                Thread.Sleep(100);
                if (NuoWa_SDK_PSD100.NP_SavePlayProgram(playProgramPtr))
                {
                    ret = -999;
                    if (NuoWa_SDK_PSD100.NP_SendPlayProgram(slIpCardInfo[IP].MacID, 0, fileName))
                    {
                        ret = WaitReturn(IP);//  发现有的时候 发送指令失败其实成功了
                        if (ret == 2)
                            ret = 0;
                        //try
                        //{
                         //  1.28 批量发送修改，发现之前总是不更新，后来屏蔽删除后，就更新了，估计之前操作中被删除文件了。
                        //  // File.Delete(fileName);
                        //}
                        //catch (Exception ex)
                        //{
                        //    LogHelper.Err("RT.Helper.Device.LED.NuoWa.NuoWa_PSD100.cs", "Send_Programs()", ex);
                        //}
                        //if(ret==
                    }
                    else
                        ret = 1;

                }
                else if (ret == -1)
                {
                    ret = 4;
                }
                else
                {
                    ret = 3;

                }
                #endregion
            }


            Close(IP);
            return ret;

        }

    }


}
